package com.shop.cereshop.app.controller.pet;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.app.annotation.NoRepeatSubmit;
import com.shop.cereshop.app.annotation.NoRepeatWebLog;
import com.shop.cereshop.app.page.pet.PetInfoVO;
import com.shop.cereshop.app.param.pet.PetSearchDTO;
import com.shop.cereshop.app.service.pet.CerePetClassifyService;
import com.shop.cereshop.app.service.pet.CerePetInfoService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.constant.IntegerEnum;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.pet.CerePetClassify;
import com.shop.cereshop.commons.domain.pet.CerePetInfo;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("pet")
@Api(value = "宠物模块", tags = "宠物模块")
@Slf4j(topic = "CerePetController")
public class CerePetController {

    @Resource
    private CerePetInfoService cerePetInfoService;

    @Resource
    private CerePetClassifyService cerePetClassifyService;

    @GetMapping("getPetInfo")
    @ApiOperation(value = "获取用户宠物信息")
    public Result<List<PetInfoVO>> getPetInfo(HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (user == null) {
            return new Result(CoReturnFormat.USER_NOT_LOGIN);
        }
        List<PetInfoVO> result = cerePetInfoService.getPetInfo(user.getBuyerUserId());
        return new Result(result, CoReturnFormat.SUCCESS);
    }

    @GetMapping("getPetClassify")
    @ApiOperation(value = "获取宠物分类")
    public Result<CerePetClassify> getPetClassify(@RequestParam(value = "classifyId", required = false) @ApiParam(value = "分类ID(查一级分类该参数为0)") Long classifyId) {
        Long queryClassifyId = (classifyId == null) ? 0 : classifyId;
        List<CerePetClassify> result = cerePetClassifyService.list(
                new LambdaQueryWrapper<CerePetClassify>()
                        .eq(CerePetClassify::getParentId, queryClassifyId)
                        .orderByAsc(CerePetClassify::getSort)
        );
        return new Result(result, CoReturnFormat.SUCCESS);
    }

    @PostMapping(value = "updatePetInfo")
    @ApiOperation(value = "更新用户宠物信息")
    @NoRepeatWebLog
    @NoRepeatSubmit
    public Result updatePetInfo(@RequestBody @NotNull(message = "参数不能为空") PetInfoVO param, HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        boolean result = cerePetInfoService.updatePetInfo(param, user);
        return result ? Result.success() : Result.failed(CoReturnFormat.FAIL);
    }

    @DeleteMapping("deletePetInfo")
    @ApiOperation(value = "删除用户宠物信息")
    @NoRepeatWebLog
    @NoRepeatSubmit
    public Result deletePetInfo(@RequestParam("petId") @NotNull(message = "参数不能为空") Long petId) {
        boolean result = cerePetInfoService.lambdaUpdate()
                .set(CerePetInfo::getState, IntegerEnum.NO.getCode())
                .eq(CerePetInfo::getPetId, petId)
                .update();
        return result ? Result.success() : Result.failed(CoReturnFormat.FAIL);
    }

    @PostMapping("getDetail")
    @ApiOperation(value = "获取宠物详情")
    public Result<PetInfoVO> getDetail(@RequestParam("petId") @NotNull(message = "参数不能为空") Long petId) {
        //根据petId查询
        PetInfoVO petInfoVO = cerePetInfoService.getDetailById(petId);
        return Result.success(petInfoVO);
    }

    @PostMapping("searchPetClassify")
    @ApiOperation(value = "搜索宠物分类")
    public Result<PageInfo<String>> searchPetClassify(@RequestBody PetSearchDTO param) {
        return null;
    }
}
